package com.goods.service.imp;


import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import com.goods.service.ProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private ProductMapper productMapper;

    //加载分类数据
    @Override
    public PageVO getCategoryTree(Integer pageNum, Integer pageSize) {
        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        if (null != productCategoryList) {
            List<ProductCategory> page = ListPageUtils.page(productCategoryList, pageSize, pageNum);
            return new PageVO<>(productCategoryList.size(), page);
        }
        return null;
    }

    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> collect = productCategoryList.stream().map(p -> {
            ProductCategoryTreeNodeVO vo = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(p, vo);
            return vo;
        }).collect(Collectors.toList());
        List<ProductCategoryTreeNodeVO> list = CategoryTreeBuilder.build(collect);
        return list;
    }

    @Override
    public void add(ProductCategory productCategory) {
        if (null != productCategory) {
            Date date = new Date();
            productCategory.setCreateTime(date);
            productCategory.setModifiedTime(date);
            productCategoryMapper.insert(productCategory);
        }
    }

    @Override
    public ProductCategory edit(Long id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        return productCategory;
    }

    @Override
    public void update(Long id, ProductCategory productCategory) {
        productCategory.setModifiedTime(new Date());
        productCategory.setId(id);
        productCategoryMapper.updateByPrimaryKey(productCategory);
    }

    @Override
    public boolean delete(Long id) {

        ProductCategory productCategory = new ProductCategory();
        productCategory.setPid(id);
        int count1 = productCategoryMapper.selectCount(productCategory);
        //有物资引用的分类
        if (count1>0) {
            return false;
        }
        //有子类
        ProductCategory productCategory1 = productCategoryMapper.selectByPrimaryKey(id);
        Long pid = productCategory1.getId();
        int count2 = productMapper.isExist(pid);
        if (count2>0) {
            return false;
        }
        int i = productCategoryMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            return true;
        }
        return false;
    }
}
